/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.io.File;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import com.boxin.ims.modules.wechat.entity.WXPay;
import com.boxin.ims.modules.wechat.service.WXPayService;

/**
 * 微信支付Controller
 * @author 肖乾
 * @version 2014-11-12
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/wXPay")
public class WXPayController extends BaseController {

	@Autowired
	private WXPayService wXPayService;
	
	@ModelAttribute
	public WXPay get(@RequestParam(required=false) Long id) {
		if (id != null){
			return wXPayService.get(id);
		}else{
			return new WXPay();
		}
	}
	
	@RequiresPermissions("wechat:wXPay:view")
	@RequestMapping(value = {"list", ""})
	public String list(WXPay wXPay, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if (!user.isAdmin()){
			wXPay.setUser(user);
		}
        Page<WXPay> page = wXPayService.find(new Page<WXPay>(request, response), wXPay); 
        model.addAttribute("page", page);
		return "modules/wechat/wXPayList";
	}

	@RequiresPermissions("wechat:wXPay:view")
	@RequestMapping(value = "form")
	public String form(WXPay wXPay, Model model) {
	
		User user = UserUtils.getUser();
		
		WXPay wx = wXPayService.getByUserId(user.getId());
		if(wx != null){//已绑定导致到列表页面
			wXPay = wx;
		}else{
			wXPay.setUser(user);
		}
		
		model.addAttribute("wXPay", wXPay);
		return "modules/wechat/wXPayForm";
	}

	@RequiresPermissions("wechat:wXPay:edit")
	@RequestMapping(value = "save")
	public String save(@RequestParam("certificateFile") MultipartFile certFile, WXPay wXPay, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, wXPay)){
			return form(wXPay, model);
		}
		
		if(certFile != null && certFile.getOriginalFilename().length()>4){  
			String productPath = "/opt/res_data/certificate/";
			FileUtils.createDirectory(productPath);
			String sname = certFile.getOriginalFilename();
			String fileType = sname.substring(sname.lastIndexOf("."));
			String fname  = "cert_user_"+UserUtils.getUser().getId()+fileType;
			File file = new File(productPath+fname);
			System.out.println("保存文件:"+productPath+fname);
			try {
				certFile.transferTo(file);
				wXPay.setCertName(file.getName());
				wXPay.setCertFile(file.getPath());
			} catch (Exception e) {
				System.out.println("上传文件失败:"+productPath+fname);
				e.printStackTrace();
			}
		}
		
		wXPayService.save(wXPay);
		addMessage(redirectAttributes, "保存微信支付'" + wXPay.getName() + "'成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/wXPay/form?repage";
	}
	
	@RequiresPermissions("wechat:wXPay:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		wXPayService.delete(id);
		addMessage(redirectAttributes, "删除微信支付成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/wXPay/?repage";
	}

}
